package com.wokeMy.view.view;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.diyview.XListView;
import com.woke.diyview.XListView.IXListViewListener;
import com.woke.method.MyApp;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.Zqdata;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import static com.zhongcai.online.R.string.user_balance_money_f4;

public class ZiqinListActivity extends Activity implements OnItemClickListener, IXListViewListener {

    private XListView mListView;
    private MyApp application;
    private RelativeLayout mBar;
    private ArrayList<Zqdata> datalist = new ArrayList<Zqdata> ();
    private int pageno = 1;//��ѯ��ҳ��
    private Myadapter adapter;
    private String user_balance_money_f;
    SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
    String defalt_df = "0.00";
    DecimalFormat ddf = new DecimalFormat (defalt_df);//保留两位小数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_zcailist);
        user_balance_money_f = getResources ().getString (user_balance_money_f4);
        intview ();

    }

    @Override
    public void onRefresh() {
        datalist.clear();
        pageno = 1;
        getlistlistdata ();
    }

    @Override
    public void onLoadMore() {
        pageno++;
        getlistlistdata();
    }

    private void intview() {
        application = (MyApp) getApplication ();
        mListView = (XListView) findViewById (R.id.avzcailist_listview);
        mListView.setPullLoadEnable (true);
        mListView.setPullRefreshEnable(true);
        mListView.setXListViewListener (this);
        adapter = new Myadapter ();
        mListView.setAdapter (adapter);
        mBar = (RelativeLayout) findViewById (R.id.avzcailist_progress_bar);

        findViewById (R.id.avzcailist_image_back).setOnClickListener (new OnClickListener () {

            @Override
            public void onClick(View v) {
                finish ();
            }
        });
        findViewById (R.id.avzcailist_text_search).setVisibility(View.GONE);
        mListView.setOnItemClickListener (this);
        getlistlistdata ();
    }

    class Myadapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return datalist.size ();
        }

        @Override
        public Object getItem(int i) {
            // TODO Auto-generated method stub
            return i;
        }

        @Override
        public long getItemId(int i) {
            // TODO Auto-generated method stub
            return i;
        }

        class Holder {
            TextView mTname;
            TextView mTmoney;
            TextView mTtime;
            TextView mTbalanceMoney;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                holder = new Holder ();
                convertView = getLayoutInflater ().inflate (R.layout.item_adter_mylist, null);
                holder.mTname = (TextView) convertView.findViewById (R.id.item_mylist_name);
                holder.mTmoney = (TextView) convertView.findViewById (R.id.item_mylist_money);
                holder.mTtime = (TextView) convertView.findViewById (R.id.item_mylist_time);
                holder.mTbalanceMoney = (TextView) convertView.findViewById (R.id.item_balance_money);
                convertView.setTag (holder);
            } else {
                holder = (Holder) convertView.getTag ();
            }

            Zqdata list = datalist.get (position);
//            String status = list.status;
            String type = list.type;
            holder.mTname.setText (list.note);

            holder.mTtime.setText (sdf.format (new Date (Integer.valueOf(list.create_time) * 1000L)));
            String mmomey=list.money;
            int endIndex =mmomey.indexOf(".");
            mmomey=mmomey.substring(0,endIndex+3);
            holder.mTmoney.setText ("+" +mmomey);
            holder.mTbalanceMoney.setText ("----");
            return convertView;
        }
    }
    private void getlistlistdata() {
        if (application.getDatas_load () == null) return;
        AsyncHttpClient ahc = new AsyncHttpClient ();
        PersistentCookieStore myCookieStore = new PersistentCookieStore (
                ZiqinListActivity.this);
        ahc.setCookieStore (myCookieStore);
        RequestParams params = new RequestParams ();
        params.put ("user_id", application.getDatas_load ().getId ());
        Log.e ("直清账单url", Constant.FMZQZDANG_URL);
        Log.e ("直清账单params", params.toString());
        ahc.post(Constant.FMZQZDANG_URL, params, new JsonHttpResponseHandler () {
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess (statusCode, headers, response);
                Log.e ("直清账单result", "response" + response);
                try {
                    String flag = response.getString ("flag");
                    if (flag.equals ("success")) {
                        JSONObject jsonObject = response.getJSONObject ("data");
                        JSONArray jsonArray = jsonObject.getJSONArray ("data");
                        Gson gson=new Gson();
                        for (int i = 0; i < jsonArray.length (); i++) {
                            Zqdata zqdata = gson.fromJson(jsonArray.getString(i), Zqdata.class);
                            pageno = pageno + 1;
                            datalist.add (zqdata);
                        }
                    } else if (flag.equals ("unknow")) {
                        String info = response.getString ("info");
                        Toast.makeText (ZiqinListActivity.this, info, Toast.LENGTH_SHORT).show ();
                    }
                } catch (JSONException e) {
                    e.printStackTrace ();
                }

                if (datalist.size () > 0) {
                    adapter.notifyDataSetChanged ();
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  String responseString, Throwable throwable) {
                super.onFailure (statusCode, headers, responseString, throwable);
                Toast.makeText (ZiqinListActivity.this, "数据失败！",
                        Toast.LENGTH_SHORT).show ();
                //				Log.e("������ǰ�dd", "ʧ��" + responseString);
            }

            @Override
            public void onStart() {
                // TODO Auto-generated method stub
                super.onStart ();

                mBar.setVisibility (View.VISIBLE);

            }

            @Override
            public void onFinish() {
                super.onFinish ();
                mListView.stopRefresh();
                mListView.stopLoadMore();
                mBar.setVisibility (View.GONE);
                //				Log.e("������ǰ�", "���");
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
//        Intent intent = new Intent (ZiqinListActivity.this, Zcailist_infoActivity.class);
//        intent.putExtra ("id", datalist.get (i - 1).id);
//        startActivity (intent);
    }
}
